package com.sens.orderback.ui.activity.mybank;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sens.common.StringUtil;
import com.sens.database.LiteOrmDBUtil;
import com.sens.ipc.mybank.IPCHelper;
import com.sens.ipc.mybank.hooker.Handle;
import com.sens.network.ApiByHttp;
import com.sens.orderback.R;
import com.sens.tables.CommonConfig;
import com.sens.tables.MybankConfig;
import com.sens.utils.ToastUtil;

public class SettingActivity extends Activity {
    private EditText bankGroupET;
    private EditText latelyTimeET;
    private EditText serviceHostET;
    private TextView toogleTV;

    private CommonConfig commonConfig;
    private MybankConfig mybankConfig;

    private Handle handle;
    private AlertDialog.Builder hintDialog;
    private boolean isStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mybank_setting);
        bankGroupET = findViewById(R.id.bankGroupET);
        latelyTimeET = findViewById(R.id.latelyTimeET);
        serviceHostET = findViewById(R.id.serviceHostET);
        toogleTV = findViewById(R.id.toogleTV);

        commonConfig = LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).queryOne(CommonConfig.class);
        mybankConfig = LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).queryOne(MybankConfig.class);

        String host = commonConfig.getServiceHost();
        if (!StringUtil.isEmpty(host)) serviceHostET.setText(host);

        String channelNumber = mybankConfig.getChannelNumber();
        if (!StringUtil.isEmpty(channelNumber)) bankGroupET.setText(channelNumber);
        String queryTimeInterval = mybankConfig.getQueryTimeInterval();
        if (!StringUtil.isEmpty(queryTimeInterval)) latelyTimeET.setText(queryTimeInterval);
        handle = IPCHelper.getHookerHandle(this, null);

        isStart = mybankConfig.isStart();
        if (isStart) toogleTV.setText("关闭模块(已经开启)");
        else toogleTV.setText("开启模块(已经关闭)");
        hintDialog = new AlertDialog.Builder(this);
        hintDialog.setCancelable(false);
        hintDialog.setTitle("确定保存修改？");
        hintDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String bankGroup = bankGroupET.getText().toString().trim();
                String latelyTime = latelyTimeET.getText().toString().trim();
                String serviceHost = serviceHostET.getText().toString().trim();
                if (StringUtil.isEmpty(latelyTime)) latelyTime = "5";
                boolean needUpdate = false;
                if (!bankGroup.equals(mybankConfig.getChannelNumber())) {
                    needUpdate = true;
                    ApiByHttp.getInstance().setChannelNumber(bankGroup);
                    mybankConfig.setChannelNumber(bankGroup);
                    handle.setChannelNumber(bankGroup);
                }
                if (!latelyTime.equals(mybankConfig.getQueryTimeInterval())) {
                    needUpdate = true;
                    ApiByHttp.getInstance().setQueryTimeInterval(latelyTime);
                    mybankConfig.setQueryTimeInterval(latelyTime);
                    handle.setQueryTimeInterval(latelyTime);
                }
                if (needUpdate)
                    LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).update(mybankConfig);
                if (!serviceHost.equals(commonConfig.getServiceHost())) {
                    ApiByHttp.getInstance().setHost(serviceHost);
                    handle.setHost(serviceHost);
                    com.sens.ipc.alipay.hooker.Handle h = com.sens.ipc.alipay.IPCHelper.getHookerHandle(SettingActivity.this, null);
                    h.setHost(serviceHost);
                    h.unBind();
                    commonConfig.setServiceHost(serviceHost);
                    LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).update(commonConfig);
                }
                Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
                setResult(RESULT_OK);
                finish();
            }
        });
        hintDialog.setNegativeButton("取消", null);
    }

    @Override
    protected void onDestroy() {
        if (handle != null) handle.unBind();
        super.onDestroy();
    }

    public void close(View view) {
        finish();
    }

    public void toogle(View view) {
        if (!isStart && !mybankConfig.isCollectionAble()) {
            ToastUtil.showToast("检测未通过，暂不能开启");
            return;
        }
        isStart = !isStart;
        if (isStart) {
            mybankConfig.setStart(true);
            handle.start();
            toogleTV.setText("关闭模块(已经开启)");
        } else {
            mybankConfig.setStart(false);
            handle.stop();
            toogleTV.setText("开启模块(已经关闭)");
        }
        LiteOrmDBUtil.getGlobalDBUtil(getApplicationContext()).update(mybankConfig);
    }

    public void submit(View view) {
        String bankGroup = bankGroupET.getText().toString().trim();
        if (StringUtil.isEmpty(bankGroup)) {
            Toast.makeText(this, "请设置银行组", Toast.LENGTH_SHORT).show();
            return;
        }
        String serviceHost = serviceHostET.getText().toString().trim();
        if (StringUtil.isEmpty(serviceHost)) {
            Toast.makeText(this, "请设置服务器地址", Toast.LENGTH_SHORT).show();
            return;
        }
        String latelyTime = latelyTimeET.getText().toString().trim();
        StringBuilder sb = new StringBuilder();
        sb.append("银行组:");
        sb.append(bankGroup);
        sb.append("\n");
        sb.append("结算最近时间:");
        if (StringUtil.isEmpty(latelyTime))
            sb.append("5");
        else
            sb.append(latelyTime);
        sb.append("\n");
        sb.append("服务器:");
        sb.append(serviceHost);
        hintDialog.setMessage(sb.toString());
        hintDialog.show();
    }
}